package org.qing.javafx.console;

import java.io.OutputStream;
import javafx.application.Platform;
import javafx.scene.control.TextArea;

@SuppressWarnings("restriction")
public class Console extends OutputStream {
	
    private TextArea console;

    public Console(TextArea console) {
        this.console = console;
    }

    @Override
    public void write(int b) {
        String text = String.valueOf((char) b);
        Platform.runLater(() -> {
            console.appendText(text);
            console.selectPositionCaret(console.getLength());
            console.deselect(); //removes the highlighting
        });
    }
    
    @Override
    public void write(byte[] b, int off, int len) {
        String s = new String(b, off, len);
        Platform. runLater(() -> {
        	console. appendText(s);
        	console.selectPositionCaret(console.getLength());
            console.deselect(); //removes the highlighting
        });
    }
}